﻿using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Stok
{
    public partial class Monti_Tip3 : XITopCekiListesi
    {
        int YuklemeId;
        public Monti_Tip3(int YuklemeId):
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            Init("Monti Tip3 Çeki Listesi", true);
            this.FileDone += this_FileDone;
            //this.SplitterWidthPercentage = 30;
        }

        void this_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                DataRowCollection rows = dt.Rows;
                DataColumnCollection cols = dt.Columns;

                Dictionary<string, int> indexes = new Dictionary<string, int>();
                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    DataRow dr0 = dt.Rows[0];
                    switch (dr0[colIndex].ToString())
                    {
                        case "KALİTE": 
                        case "CODE": indexes.Add("Kalite", colIndex); break;
                        case "DESIGN": indexes.Add("DesenNo", colIndex); break;
                        case "COLOR": indexes.Add("Color", colIndex); break;
                        case "PIECE NO.": 
                        case "ROLL NO.": indexes.Add("VendorRollId", colIndex); break;
                        case "MTR.": 
                        case "METER": indexes.Add("Qty", colIndex); break;
                    }
                }

                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    DataRow dr = rows[i];
                    string article = dr[indexes["Kalite"]].ToString().PadLeft(3, '0') +
                        "-" + dr[indexes["DesenNo"]].ToString().PadLeft(4, '0') +
                        "/" + dr[indexes["Color"]].ToString().PadLeft(3, '0');

                    if (article != "000-0000/000")
                    {
                        if (Article.Select("Article='" + article + "'").Length == 0)
                        {
                            DataRow drArticle = Article.NewRow();
                            drArticle["Article"] = article;
                            Article.Rows.Add(drArticle);
                        }
                        DataRow drRoll = Roll.NewRow();
                        drRoll["Article"] = article;
                        drRoll["VendorRollId"] = dr[indexes["VendorRollId"]];
                        drRoll["Qty"] = dr[indexes["Qty"]];
                        //drRoll["WidthUsable"] = dr[indexes["WidthUsable"]];

                        Roll.Rows.Add(drRoll);
                    }
                }
            }
        }
    }
}
